home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C++ / Frameworks / Sprocket Framework DR2 / Sprocket Framework / UDialog.cp < prev    next >
Text File  |  1996-06-15  |  11KB  |  520 lines

  1. /*
  2.  
  3.     File:        UDialog.cp
  4.     Project:    Sprocket Framework 1.1 (DR2), released 6/15/96
  5.     Contains:    Dialog utilities
  6.     To Do:        ?
  7.  
  8.     Sprocket Major Contributors:
  9.     ----------------------------
  10.     Dave Falkenburg, producer of Sprocket 1.0
  11.     Bill Hayden,     producer of Sprocket 1.1
  12.     Steve Sisak,     producer of the upcoming Sprocket 2.0
  13.     
  14.     Pete Alexander        Steve Falkenburg    Randy Thelen
  15.     Eric Berdahl        Nitin Ganatra        Chris K. Thomas
  16.     Marshall Clow        Dave Hershey        Leonard Rosenthal
  17.     Tim Craycroft        Dave Mark            Dean Yu
  18.     David denBoer        Gary Powell
  19.     Cameron Esfahani    Jon Summers            Apple Computer, Inc.
  20.         
  21.     Comments, Additions, or Corrections:
  22.     ------------------------------------
  23.     Bill Hayden, Nikol Software <nikol@codewell.com>
  24.  
  25. */
  26.  
  27. #include "Sprocket.h"
  28. #include "UDialog.h"
  29. #include "UString.h"
  30.  
  31. #ifndef __PROCESSES__
  32. #include <Processes.h>
  33. #endif
  34.  
  35. #ifndef __THREADS__
  36. #include <Threads.h>        //    For YieldToAnyThread()
  37. #endif
  38.  
  39.  
  40. #ifndef __STANDARDFILE__
  41. #include <StandardFile.h>    //    For ModalFilterYDProcPtr
  42. #endif
  43.  
  44. //    Some types which should probably be defined in <Dialogs.h>
  45. //    NOTE: These must be aligned on 2-byte boundaries
  46.  
  47. #if PRAGMA_ALIGN_SUPPORTED
  48. #pragma    push
  49. #pragma options align=mac68k
  50. #endif
  51.  
  52. struct DialogItem
  53.     {
  54.     long    usedByDialogManager;
  55.     Rect    boundsRect;
  56.     char    type;
  57.     char    length;
  58.     };
  59.  
  60. struct DialogItemList            //    a.k.a. a 'DITL'
  61.     {
  62.     short        count;
  63.     DialogItem    firstItem[1];
  64.     };
  65.  
  66. //    Restore default alignment
  67.  
  68. #if PRAGMA_ALIGN_SUPPORTED
  69. #pragma pop
  70. #endif
  71.  
  72.  
  73. typedef    DialogItem        *DialogItemPtr;
  74. typedef    DialogItemList    **DialogItemListHandle;
  75. typedef    DialogTemplate    **DialogTemplateHandle;
  76.  
  77.  
  78. //    private function Prototypes
  79.  
  80.         pascal    Boolean    StandardDialogFilterProc(DialogRef theDialog, EventRecord * anEvent, short * itemHit);
  81. static    pascal    Boolean    StandardDialogFilterYDProc(DialogRef theDialog, EventRecord * anEvent, short * itemHit, void * yourData);
  82. static    pascal    Boolean    StandardCloseDialogFilterProc(DialogRef theDialog, EventRecord * anEvent, short * itemHit);
  83. static            Boolean    FilterProcCommon(DialogRef theDialog, EventRecord * anEvent, short * itemHit);
  84.  
  85.  
  86.  
  87. ///////////////////////////////////////////////////////////
  88. //
  89. //    StandardAlert
  90. //
  91. //    An alternative to Alert() which uses the extended
  92. //    Dialog Manager capabilities.
  93. //
  94. //    I’m not sure we really need this call, but it seems
  95. //    to do the trick just fine.
  96.  
  97. short
  98. StandardAlert(    short dlogID,
  99.                 short defaultItem,                /* = ok */
  100.                 short cancelItem,                /* = 0 */
  101.                 ModalFilterUPP customFilterUPP    /* = nil */)
  102. {
  103.     DialogRef        theDialog;
  104.     short            itemHit = 0;
  105.     ModalFilterUPP    filterToUse;
  106.     
  107.     HiliteWindowsForModalDialog(false);
  108.  
  109.     theDialog = GetNewDialog(dlogID,nil,(WindowRef) -1);
  110.     if (defaultItem)
  111.         SetDialogDefaultItem(theDialog,defaultItem);
  112.     if (cancelItem)
  113.         SetDialogCancelItem(theDialog,cancelItem);
  114.  
  115.     if (customFilterUPP)
  116.         filterToUse = customFilterUPP;
  117.     else
  118.         filterToUse = StandardDialogFilterUPP;
  119.  
  120.     do
  121.         ModalDialog(filterToUse,&itemHit);
  122.     while (itemHit == 0);
  123.     
  124.     DisposeDialog(theDialog);
  125.  
  126.     HiliteWindowsForModalDialog(true);
  127.  
  128.     return itemHit;
  129. }
  130.  
  131.  
  132.  
  133.  
  134. /*****************************************************************************/
  135.  
  136.  
  137.  
  138. ///////////////////////////////////////////////////////////
  139. //
  140. //    ErrorAlert
  141. //
  142. //    A nice error reporting routine which presents an
  143. //    alert box containing the supplied text.
  144. //
  145.  
  146. void ErrorAlert(short stringList, short whichString, Boolean fatalError)
  147. {
  148.     Str255                    errorString;
  149.     const StringPtr            nullStr = (StringPtr) "\p";
  150.  
  151.  
  152.     GetIndString(errorString, stringList, whichString);
  153.     SetCursor(&qd.arrow);
  154.     ParamText(errorString, nullStr, nullStr, nullStr);
  155.  
  156.     (void) StandardAlert(kErrorAlertID);
  157.     
  158.     if (fatalError)
  159.         ExitToShell();
  160. }
  161.  
  162.  
  163.  
  164. /*****************************************************************************/
  165.  
  166.  
  167.  
  168. ///////////////////////////////////////////////////////////
  169. //
  170. //    ErrorReporter
  171. //
  172. //    A nice error reporting routine which presents an
  173. //    alert box containing the supplied the error, as well
  174. //  as the file and line it occurred on.
  175. //
  176.  
  177. void ErrorReporter(OSErr err, char* file, long line)
  178. {
  179.     Str255    errStr, lineStr;
  180.     char    filename[255];
  181.     
  182.     NumToString(err, errStr);
  183.     NumToString(line, lineStr);
  184.     ccpy(filename, file);
  185.     c2p(filename);
  186.  
  187. #if qDebug
  188.     //SysBreakStr("\pError State: ErrorReporter Called");
  189. #endif
  190.     
  191.     ParamText(errStr, (StringPtr)filename, lineStr, "\p");
  192.     
  193.     SetCursor(&qd.arrow);
  194.     StandardAlert(kErrorReporterAlertID);
  195. }
  196.  
  197.  
  198.  
  199. /*****************************************************************************/
  200.  
  201.  
  202.  
  203. ///////////////////////////////////////////////////////////
  204. //
  205. //    StandardDialogFilter and StandardDialogFilterYD
  206. //
  207. //    These function takes care of routing events not meant
  208. //    for the dialog window to other parts of the application.
  209. //
  210. //    Use them as an alternative to passing a NIL ModalFilterProc
  211. //    to ModalDialog() and CustomGet(Put)File. Unlike the default
  212. //    filter, these routines properly processes update events
  213. //    to keep background processes running.
  214. //
  215. //    The Thread Manager, if present, is also called to yield
  216. //    control to other cooperative threads within the process.
  217. //
  218. //    Because of pascal calling conventions we need two separate
  219. //    routines, but this is minimized by sharing implementation
  220. //    in FilterProcCommon.
  221.  
  222.  
  223. ModalFilterUPP    StandardDialogFilterUPP
  224. = NewModalFilterProc(StandardDialogFilterProc);
  225.  
  226. ModalFilterYDUPP    StandardDialogFilterYDUPP
  227. = NewModalFilterYDProc(StandardDialogFilterYDProc);
  228.  
  229.  
  230. /*****************************************************************************/
  231.  
  232.  
  233.  
  234. pascal Boolean StandardDialogFilterProc(DialogRef theDialog, EventRecord* anEvent, short* itemHit)
  235. {
  236.     //    Call through common code to check for events we’d like to handle.
  237.     //    If that is unsuccessful, call through the System 7 StdFilterProc
  238.     //    to handle CR, “CMD-.” & ESC in an international-friendly manner.
  239.  
  240.     if (FilterProcCommon(theDialog, anEvent, itemHit))
  241.         return true;
  242.     else
  243.         return (StdFilterProc(theDialog, anEvent, itemHit));
  244. }
  245.  
  246.  
  247. /*****************************************************************************/
  248.  
  249.  
  250. pascal Boolean StandardDialogFilterYDProc(DialogRef theDialog, EventRecord* anEvent, short* itemHit, void * /*unusedData*/)
  251. {
  252.     //    We don’t call through to StdFilterProc since the
  253.     //    Standard File Package already does everything we need.
  254.  
  255.     return FilterProcCommon(theDialog, anEvent, itemHit);
  256. }
  257.  
  258.  
  259.  
  260. /*****************************************************************************/
  261.  
  262.  
  263.  
  264. void PseudoClickInDialogItem(DialogRef theDialog, short itemToClick)
  265. {
  266.     ControlRef    itemHandle;
  267.     Rect        itemBox;
  268.     long        finalTicks;
  269.     short        itemType;
  270.     
  271.     GetDialogItem(theDialog, itemToClick, &itemType, (Handle *) &itemHandle, &itemBox);
  272.  
  273.     HiliteControl( itemHandle, kControlButtonPart );
  274.     Delay(8, &finalTicks);
  275.     HiliteControl( itemHandle, 0 );
  276. }
  277.  
  278.  
  279. /*****************************************************************************/
  280.  
  281.  
  282.  
  283. Boolean ToggleCheckBox(DialogRef theDialog, short theCheckBox)
  284. {
  285.     ControlRef    itemHandle;
  286.     Rect        itemBox;
  287.     short        itemType;
  288.     
  289.     
  290.     GetDialogItem(theDialog, theCheckBox, &itemType, (Handle *) &itemHandle, &itemBox);
  291.     
  292. #if qDebug
  293.     if (itemType < 4 || itemType > 7)
  294.         {
  295.         DebugMessage("\pToggleCheckBox called with non-togglable item");
  296.         return false;
  297.         }
  298. #endif
  299.  
  300.     if (GetControlValue(itemHandle) == 1)
  301.         {
  302.         SetControlValue(itemHandle, 0);
  303.         return false;
  304.         }
  305.     else
  306.         {
  307.         SetControlValue(itemHandle, 1);
  308.         return true;
  309.         }
  310. }
  311.  
  312.  
  313.  
  314. /*****************************************************************************/
  315.  
  316.  
  317.  
  318.  
  319. void SetControlActive(DialogRef theDialog, short itemHit, Boolean hilite)
  320. {
  321.     short    iType;
  322.     Handle    iHandle;
  323.     Rect    iRect;
  324.             
  325.     GetDialogItem(theDialog, itemHit, &iType, &iHandle, &iRect);
  326.     HiliteControl( (ControlRef)iHandle, hilite ? 0 : 255 );
  327. }
  328.  
  329.  
  330.  
  331. /*****************************************************************************/
  332.  
  333.  
  334.  
  335.  
  336. short GetControlSetting(DialogRef theDialog, short itemHit)
  337. {
  338.     short            iType;
  339.     ControlRef        iHandle;
  340.     Rect            iRect;
  341.             
  342.     GetDialogItem(theDialog, itemHit, &iType, (Handle *)&iHandle, &iRect);
  343.     return GetControlValue(iHandle);
  344. }
  345.  
  346.  
  347.  
  348. /*****************************************************************************/
  349.  
  350.  
  351.  
  352.  
  353. void SetControlSetting(DialogRef theDialog, short itemHit, short setting)
  354. {
  355.     short            iType;
  356.     ControlRef        iHandle;
  357.     Rect            iRect;
  358.             
  359.     GetDialogItem(theDialog, itemHit, &iType, (Handle *)&iHandle, &iRect);
  360.     SetControlValue(iHandle, setting);
  361. }
  362.  
  363.  
  364.  
  365. /*****************************************************************************/
  366.  
  367.  
  368.  
  369.  
  370. void GetControlRange(DialogRef theDialog, short itemHit, short *min, short *max)
  371. {
  372.     short            iType;
  373.     ControlRef        iHandle;
  374.     Rect            iRect;
  375.             
  376.     GetDialogItem(theDialog, itemHit, &iType, (Handle *)&iHandle, &iRect);
  377.     *min = GetControlMinimum(iHandle);
  378.     *max = GetControlMaximum(iHandle);
  379. }
  380.  
  381.  
  382.  
  383. /*****************************************************************************/
  384.  
  385.  
  386.  
  387.  
  388. void SetControlRange(DialogRef theDialog, short itemHit, short min, short max)
  389. {
  390.     short            iType;
  391.     ControlRef        iHandle;
  392.     Rect            iRect;
  393.             
  394.     GetDialogItem(theDialog, itemHit, &iType, (Handle *)&iHandle, &iRect);
  395.     SetControlMinimum(iHandle, min);
  396.     SetControlMaximum(iHandle, max);
  397. }
  398.  
  399.  
  400.  
  401. /*****************************************************************************/
  402.  
  403.  
  404.  
  405. void SetEditText(DialogRef theDialog, short itemHit, ConstStr255Param textStr)
  406. {
  407.     short    iType;
  408.     Handle    iHandle;
  409.     Rect    iRect;
  410.  
  411.             
  412.     GetDialogItem(theDialog, itemHit, &iType, &iHandle, &iRect);
  413.  
  414. #if qDebug
  415.     if (iType != statText && iType != editText)
  416.         {
  417.         DebugMessage("\pSetEditText called with non-text item");
  418.         return;
  419.         }
  420. #endif
  421.  
  422.     SetDialogItemText( iHandle, textStr );
  423. }
  424.  
  425.  
  426.  
  427. /*****************************************************************************/
  428.  
  429.  
  430.  
  431. void GetEditText(DialogRef theDialog, short itemHit, StringPtr textStr)
  432. {
  433.     short    iType;
  434.     Handle    iHandle;
  435.     Rect    iRect;
  436.             
  437.  
  438.     GetDialogItem(theDialog, itemHit, &iType, &iHandle, &iRect);
  439.  
  440. #if qDebug
  441.     if (iType != statText && iType != editText)
  442.         {
  443.         DebugMessage("\pGetEditText called with non-text item");
  444.         return;
  445.         }
  446. #endif
  447.  
  448.     GetDialogItemText( iHandle, textStr );
  449. }
  450.  
  451.  
  452.  
  453. /*****************************************************************************/
  454.  
  455.  
  456.  
  457. Boolean FilterProcCommon(DialogRef theDialog, EventRecord * anEvent, short * /* itemHit */)
  458. {
  459.     switch (anEvent->what)
  460.         {
  461.         case updateEvt:
  462.         case activateEvt:
  463.             //     Update or activate for the dialog window?
  464.             
  465.             if (theDialog == (DialogRef) anEvent->message)
  466.                 break;
  467.  
  468.             //    no, fall through to HandleEvent            
  469.             
  470.         case diskEvt:
  471.             HandleEvent(anEvent);
  472.             return(false);
  473.  
  474.         default:
  475.             break;        
  476.         }
  477.  
  478.     if (gHasThreadManager)        //    If we have threads, let them run!
  479.         YieldToAnyThread();
  480.  
  481.     return false;                //    We didn’t handle the event
  482. }
  483.  
  484.  
  485.  
  486. /*****************************************************************************/
  487.  
  488.  
  489.  
  490. void SetDialogFontAndSize ( DialogRef theDialog, short fontNum, short fontSize )
  491. {
  492.     GrafPtr        savePort;
  493.     TEHandle    teh = GetDialogTextEdit ( theDialog );
  494.     
  495.     GetPort ( &savePort );
  496.     SetGrafPortOfDialog ( theDialog );
  497.     
  498.     //    set up the port info
  499.     
  500.     TextFont ( fontNum );
  501.     TextSize ( fontSize );
  502.  
  503.     //    now deal with the static & edit text issues
  504.     
  505.     if ( teh != NULL )
  506.         {
  507.         FontInfo    f;
  508.         TEPtr        pText;
  509.     
  510.         GetFontInfo ( &f );
  511.         pText = *teh;
  512.         pText->txFont        = applFont;
  513.         pText->txSize        = fontSize;
  514.         pText->lineHeight    = f.ascent + f.descent + f.leading;
  515.         pText->fontAscent    = f.ascent;
  516.         }
  517.     
  518.     SetPort ( savePort );
  519. }
  520.